$(document).ready(function() {
    $("fieldset input").blur(function() {
        validField(this);
    });
    $(".sidebar-clickable-cell").mouseover(function() {
        $(this).css("background", "white");
    });
    $(".sidebar-clickable-cell").mouseout(function() {
        $(this).css("background", "#404343");
    });

    $(".sidebar-clickable-cell").mousedown(function() {
        var link = $(this).children("ul").children("li").children("a")
        $(link).css("color", "red");
        window.location.replace($(link).attr("href"));
    });
});

function validField(fld) {
    var html = "<ul id='" + $(fld).attr("name") +"-errors'>";
    if ($(fld).val()) {
        var arr = {}
        arr[$(fld).attr("name")] = $(fld).val();
        $.postJSON($(fld).attr("name"), arr, function(resp) {
            if (resp.state=="ERR") {
                for(var i=0; i<resp.errCount; i++) {
                    html += "<li>" + resp.msg[i]+ "</li>";
                }
                html += "</ul>";
                $("#" + $(fld).attr("name") + "-errors").replaceWith(html);
            } else {
                html += "<div style='display: block'/></ul>";
                $("#" + $(fld).attr("name") + "-errors").replaceWith(html);
            }
        });
    } else {
        html += "<div style='display: block'/></ul>";
        $("#" + $(fld).attr("name") + "-errors").replaceWith(html);
    }
}